<?php

!defined('IN_NOVA') && exit('Access Denied!');

class Request {

	public function __construct() {
		// url 处理
		$len = strlen( APP_PATH ) - strlen( $_SERVER['REQUEST_URI'] );
		if( $len == 0 ) {
			$_GET['URL'] = explode( '/', '' );
		} else {
			$_GET['URL'] = explode( '/', substr( $_SERVER['REQUEST_URI'], $len ) );
		}

		// 一些安全处理
		if ( !get_magic_quotes_gpc() ) {
			$_GET = $this->add_slashes( $_GET );
			$_POST = $this->add_slashes( $_POST );
			$_COOKIE = $this->add_slashes( $_COOKIE );
		}
	}

    function __set( $name, $value ) {
        $_GET[$name] = $value;
    }

    function __get( $name ) {
        return $this->get( $name, 'G', 'int' );
    }

	// 获取 $_GET、$_POST、$_COOKIE、$_SERVER值
	public function get( $var, $method = 'G', $type = 'string' ) {
		//[DEBUG]
		global $log;
		//[/DEBUG]
		$temp = NULL;
		switch( $method ) {
			case 'G':
				$temp = isset($_GET[$var]) ? $_GET[$var] : NULL;
				break;
			case 'P':
				$temp = isset($_POST[$var]) ? $_POST[$var] : NULL;
				break;
			case 'C':
				$temp = isset($_COOKIE[$var]) ? $_COOKIE[$var] : NULL;
				break;
			case 'S':
				$temp = isset($_SERVER[$var]) ? $_SERVER[$var] : NULL;
				break;
		}
		//[DEBUG]
		if( $temp == NULL && defined( 'APP_DEBUG' ) && APP_DEBUG === TRUE ) {
			$log->add( '[' . __FILE__ . '] [' . __LINE__ . '] ' . L( '_USE_ILLEGAL_INDEX_' ) . ' ' . $var, E_USER_NOTICE );
		}
		//[/DEBUG]
		switch( $type ) {
			case 'int':
				//[DEBUG]
				if( defined( 'APP_DEBUG' ) && APP_DEBUG === TRUE ) {
					if( intval( $temp ) != $temp ) {
						$log->add( '[' . __FILE__ . '] [' . __LINE__ . '] ' . L( '_SUBMIT_ILLEGAL_PARAMETERS_' ) . $temp, E_USER_NOTICE );
					}
				}
				//[/DEBUG]
				if( $temp == NULL ) {
					$temp = 0;
				}
				$temp = intval( $temp );
				break;
			case 'array':
				if( !is_array( $temp ) ) {
					$temp = array();;
				}
				break;
			case 'string':
				// 待添加
				$temp = $temp;
				break;
		}
		return $temp;
	}

	// 获得访客 ip
	static public function get_ip() {
		if( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/',$_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
			return $_SERVER['HTTP_X_FORWARDED_FOR'];
		} elseif ( isset( $_SERVER['HTTP_CLIENT_IP'] ) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/',$_SERVER['HTTP_CLIENT_IP'] ) ) {
			return $_SERVER['HTTP_CLIENT_IP'];
		} elseif ( isset( $_SERVER['REMOTE_ADDR'] ) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/',$_SERVER['REMOTE_ADDR'] ) ) {
			return $_SERVER['REMOTE_ADDR'];
		} else {
			return 'Unknown';
		}
	}

	static public function get_http_referer() {
		if( isset( $_SERVER['HTTP_REFERER'] ) ){
			return $_SERVER['HTTP_REFERER'];
		}else{
			return '';
		}
	}

	// ----------------------------------------
	// 以下是私有函数

	private function add_slashes( $string ) {
		if ( !is_array( $string ) ) return addslashes( $string );
		foreach ( $string as $key => $val ) {
			$string[$key] = $this->add_slashes( $val );
		}
		return $string;
	}

	private function del_slashes($string) {
		if ( !is_array( $string ) ) return stripslashes( $string );
		foreach ( $string as $key => $val ) {
			$string[$key] = $this->del_slashes( $val );
		}
		return $string;
	}

}
?>
